package com.example.exam.controller;

import com.example.exam.entity.ExamPaper;
import com.example.exam.entity.ExamPaperAnswer;
import com.example.exam.service.*;
import com.example.exam.util.ResultVOUtil;
import com.example.exam.vo.ExamAnswerDetailVO;
import com.example.exam.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author shishuqian
 * date 2021/5/2
 * time 20:40
 **/
@RestController
@RequestMapping("/examPaperAnswer")
@Api(value = "用户填写试卷答案controller",tags = {"用户填写试卷接口"})
public class ExamPaperAnswerController {

    @Autowired
    private ExamPaperAnswerService examPaperAnswerService;

    @Autowired
    private ExamPaperService examPaperService;

    @Autowired
    private ExamAnswerDetailService examAnswerDetailService;

    @Autowired
    private CorrectExamPaperService correctExamPaperService;

    @Autowired
    private ExamUserService examUserService;

    @PostMapping("/save")
    @ApiOperation(value = "保存用户填写的试卷答案")
    public ResultVO save(@RequestBody ExamPaperAnswer examPaperAnswer){

        Integer userId = examPaperAnswer.getUserId();
        Integer examPaperId = examPaperAnswer.getExamPaperId();
        List<ExamPaperAnswer> answers = examPaperAnswerService.findByUserIdAndExamPaperId(userId, examPaperId);

        //提交过了，还想创建新试卷提交的话直接拦截
        if (examPaperAnswer.getId()==null && !answers.isEmpty()){
            return ResultVOUtil.error("已经提交过了，不能重复提交...");
        }

        //第一次填写时
        examPaperAnswer.setCreateTime(new Date());
        examPaperAnswer.setStatus(0);  //批改状态 0:未批改

        ExamPaper examPaper = examPaperService.findById(examPaperId);
        if (examPaper.getSelectPeople()!=null && examPaper.getSelectPeople()) { //如果填写的是选人试卷
            if (examPaperAnswer.getFinishStatus()!=null && examPaperAnswer.getFinishStatus()){  //提交结束
                examUserService.setFilled(examPaperId,userId);
            }
        }

        //保存
        int id = examPaperAnswerService.save(examPaperAnswer);
        //考生完成提交后系统批改
        correctExamPaperService.systemCorrect(id);

        ExamPaperAnswer answer = examPaperAnswerService.findById(id);
        return ResultVOUtil.success(answer);
    }

    @GetMapping("/findByUserIdAndPaperId/{userId}/{paperId}")
    public ResultVO findByUserIdAndPaperId(@PathVariable("userId") Integer userId,
                                           @PathVariable("paperId") Integer paperId){
        List<ExamPaperAnswer> answer = examPaperAnswerService.findByUserIdAndExamPaperId(userId, paperId);
        if (answer.isEmpty()){
            return ResultVOUtil.success(null);
        }else{
            ExamPaperAnswer answer1 = answer.get(0);
            return ResultVOUtil.success(answer1);
        }
    }


    @ApiOperation(value = "查询考试结果")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = ExamAnswerDetailVO.class)})
    @GetMapping("/queryExamResult/{examPaperAnswer_id}/{teacherSee}")
    public ResultVO findById(@PathVariable("examPaperAnswer_id") Integer examPaperAnswer_id,
                             @PathVariable("teacherSee") String teacherSee){



        ExamAnswerDetailVO examAnswerDetail = examAnswerDetailService.getExamAnswerDetail(examPaperAnswer_id);
        if (teacherSee.equals("teacher")){  //如果是出卷人查看，直接允许
            return ResultVOUtil.success(examAnswerDetail);
        }
       //以下为做卷人查看
        ExamPaperAnswer examPaperAnswer = examPaperAnswerService.findById(examPaperAnswer_id);


        Integer examPaperId = examPaperAnswer.getExamPaperId();

        ExamPaper examPaper = examPaperService.findById(examPaperId);
        Date endTime = examPaper.getEndTime();  //获取试卷结束时间

        //试卷提交后不允许查看
        if (examPaper.getViewAfterSubmit() != null && !examPaper.getViewAfterSubmit()){
            return ResultVOUtil.error("该试卷提交后不允许查看");
        }
        //考试未结束不能查看
            Date date = new Date(); //当前时间
            if (endTime != null){
                if (date.before(endTime)){
                    return ResultVOUtil.error("考试未结束，不能查看试卷...");
                }
            }

            //未完成考试不能查看
        if (examPaperAnswer.getFinishStatus() == null || !examPaperAnswer.getFinishStatus()) {
            return ResultVOUtil.error("你的考试尚未完成，不能查看考试结果...");
        }

        return ResultVOUtil.success(examAnswerDetail);
//        if (examPaperAnswer.getStatus().equals(2)){
//            ExamAnswerDetailVO examAnswerDetail = examAnswerDetailService.getExamAnswerDetail(examPaperAnswer_id);
//            return ResultVOUtil.success(examAnswerDetail);
//        }else {
//            return ResultVOUtil.error("该试卷未批改完毕...");
//        }

    }




    @ApiOperation(value = "查询考试记录")
    @GetMapping("/findExamPaperAnswers/{user_id}/{page}/{size}")
    public ResultVO findExamPaperAnswers(@PathVariable("user_id") Integer user_id,
                                         @PathVariable("page") Integer page,
                                         @PathVariable("size") Integer size){

        Page<ExamPaperAnswer> examPaperAnswerVOList = examPaperAnswerService
                .findExamPaperAnswersByUserId(user_id,page,size);

        return ResultVOUtil.success(examPaperAnswerVOList);

    }

    //查询要批改,根据创建时间升序排列
    @GetMapping("/getCheck/{examPaperId}/{page}/{size}")
    public ResultVO getCheck(@PathVariable("examPaperId") Integer examPaperId,
                             @PathVariable("page") Integer page,
                             @PathVariable("size") Integer size){
        Page<ExamPaperAnswer> check = examPaperAnswerService.getCheck(examPaperId, 1,page,size);
        return ResultVOUtil.success(check);
    }

    //查询已批改的试卷,根据创建时间升序排列
    @GetMapping("/getChecked/{examPaperId}/{page}/{size}")
    public ResultVO getChecked(@PathVariable("examPaperId") Integer examPaperId,
                               @PathVariable("page") Integer page,
                               @PathVariable("size") Integer size){
        Page<ExamPaperAnswer> check = examPaperAnswerService.getCheck(examPaperId, 2,page,size);
        return ResultVOUtil.success(check);
    }

    @GetMapping("/findByPaperNameLike/{paperName}/{userId}/{page}/{size}")
    public ResultVO findByPaperNameLike(@PathVariable("paperName") String paperName,
                                    @PathVariable("userId") Integer userId,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size){
        Page<ExamPaperAnswer> answers = examPaperAnswerService
                .findByPaperNameLike("%" + paperName + "%", userId,page, size);

        return ResultVOUtil.success(answers);
    }

    @GetMapping("/filledPeople/{examPaperId}")
    public ResultVO findNamesByPaperId(@PathVariable("examPaperId") Integer examPaperId){
        ExamPaper examPaper = examPaperService.findById(examPaperId);
        if (examPaper.getSelectPeople()!=null && examPaper.getSelectPeople()) { //如果是选人试卷
            List<String> filledNames = examUserService.findFillByExamPaperId(examPaperId, true);
            return ResultVOUtil.success(filledNames);
        }
        List<String> names = examPaperAnswerService.findNamesByPaperId(examPaperId);
        return ResultVOUtil.success(names);
    }


}
